package net.ch2.pc12.tech.ackborn.activity;

import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.text.util.Linkify;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.os.Bundle;
import android.os.Handler;

import net.ch2.pc12.tech.ackborn.R;
import net.ch2.pc12.tech.ackborn.activity.helper.MenuHelper;
import net.ch2.pc12.tech.ackborn.activity.thread.ArticleListThread;
import net.ch2.pc12.tech.ackborn.dto.ProgressDTO;
import net.ch2.pc12.tech.ackborn.model.BbsArticleModel;
import net.ch2.pc12.tech.ackborn.model.BbsThreadModel;

public class ThreadActivity extends Activity implements Runnable,OnClickListener{
    /** Called when the activity is first created. */

	private ProgressDTO progressDTO;

	private ConcurrentLinkedQueue<BbsArticleModel> articleQueue;
	
	public ConcurrentLinkedQueue<BbsArticleModel> getArticleQueue() {
		return articleQueue;
	}

	public void setArticleQueue(ConcurrentLinkedQueue<BbsArticleModel> articleQueue) {
		this.articleQueue = articleQueue;
	}

	String threadId;
	String boardUrl;
	String threadName;
	String datName;
	String boardId;
	
	public ProgressDTO getProgressDTO() {
		return progressDTO;
	}

	public void setProgressDTO(ProgressDTO progressDTO) {
		this.progressDTO = progressDTO;
	}

	TextView statusView;
	
	private BbsThreadModel model;
	ArticleListThread thread;
	private final Handler handler = new Handler();
	
	public BbsThreadModel getModel() {
		return model;
	}

	public void setModel(BbsThreadModel model) {
		this.model = model;
	}

	private TableLayout table;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.thread);
    	
    	Intent intent = getIntent();
    	threadId = intent.getStringExtra("threadId");
    	boardUrl = intent.getStringExtra("boardUrl");
    	threadName = intent.getStringExtra("threadName");
    	datName = intent.getStringExtra("datName");
    	boardId = intent.getStringExtra("boardId");
    	
    	model = new BbsThreadModel();
    	model.setBoardId(boardId);
    	model.setDatName(datName);
    	model.setThreadId(threadId);
    	model.setThreadName(threadName);
    	model.setBoardUrl(boardUrl);
     	
    	statusView = (TextView)findViewById(R.id.ArticleStatus);   	
        table = (TableLayout) findViewById(R.id.ThreadLayout);

        statusView.setText(threadName);
        articleQueue = new ConcurrentLinkedQueue<BbsArticleModel>();
        
        if(model.hasCache(this)){
        	setList(false);
        }else{
        	// 確認ダイアログだして、取得するかを聞く
        	Builder builder = new Builder(this);
        	builder.setTitle(R.string.NetworkConfirm);
        	builder.setMessage(R.string.ThreadConfirm);
        	builder.setPositiveButton(R.string.ConfirmOK, this);
        	
        	builder.setNegativeButton(R.string.ConfirmNG,this);
        	
        	builder.setCancelable(false);
        	final AlertDialog dialog = builder.create();
        	dialog.show();
        }
    }
	
	private void setList(boolean forceReload){
		progressDTO = new ProgressDTO();
		progressDTO.setProgress(0);
		statusView.setText(R.string.LoadStart);
		thread = new ArticleListThread(handler,this,forceReload);
		thread.start();
	}

    private void appendRow(BbsArticleModel article) {
        TableRow row = new TableRow(this);
        TextView num = new TextView(this);
        num.setText(article.getNum());
        num.setClickable(true);
        num.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				
			}
        	
        });

        TextView name = new TextView(this);
        name.setText(article.getName());

        TextView mail = new TextView(this);
        mail.setText("[" + article.getMail() + "]");
        mail.setClickable(true);
        mail.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				
			}
        	
        });
        
        TextView date = new TextView(this);
        date.setText(article.getDate());
        
        TextView aid = new TextView(this);
        aid.setText(article.getId());
        aid.setClickable(true);
        num.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				
			}
        	
        });
        
        row.addView(num, new TableRow.LayoutParams());
        row.addView(name, new TableRow.LayoutParams());
        row.addView(mail, new TableRow.LayoutParams());
        row.addView(date, new TableRow.LayoutParams());
        row.addView(aid, new TableRow.LayoutParams());
        
        table.addView(row, new TableLayout.LayoutParams());
        
        TableRow row2 = new TableRow(this);

        TextView body = new TextView(this);
        body.setText(article.getContents());
        body.setMaxLines(30);
        body.setMaxWidth(100);
        body.setPadding(0,2,0,4);
        body.setAutoLinkMask(Linkify.ALL);
        body.setLinksClickable(true);
        
        TableRow.LayoutParams params = new TableRow.LayoutParams();
        params.span = 5;
        row2.addView(body, params);

        table.addView(row2, new TableLayout.LayoutParams());        
    }	

	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		boolean result = super.onCreateOptionsMenu(menu);
		MenuHelper.setMenu(menu, MenuHelper.TYPE_ARTICLE_LIST);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		
		case MenuHelper.MENU_TO_TOP:
			toTop();
			return true;
		case MenuHelper.MENU_TO_BOTTOM:
			toBottom();
			return true;
		case MenuHelper.MENU_RELOAD:
			reload();
			return true;
		case MenuHelper.MENU_POST:
			post();
			return true;
		case MenuHelper.MENU_ADD_BOOKMARK:
			bookmark();
			return true;
		case MenuHelper.MENU_CONFIG:
			config();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void toTop(){

	}
	
	private void toBottom(){

	}
	
	private void reload(){

	}
	
	private void bookmark(){
		
	}
	
	private void config(){

	}
	
	private void post(){

	}

	private void display(){
		BbsArticleModel m = null;
		while((m = articleQueue.poll()) != null){
			appendRow(m);
		}
/*		
		List<BbsArticleModel> list = model.getArticleModelList();
		
		for(int i=0;i<list.size();i++){
			appendRow(list.get(i));
		}
*/	
	}
	
	public void run() {
		statusView.setText(progressDTO.getProgressMessage());
		display();
		if(progressDTO.getProgress() == ProgressDTO.COMPLETE){
			display();
			statusView.setText(threadName);
		}
	}

	public void onClick(DialogInterface dialog, int which) {
		if(which == DialogInterface.BUTTON_POSITIVE){
			setList(true);
		}
		dialog.dismiss();
	}

}